package com.japp.sys.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.japp.base.BaseAction;
import com.japp.sys.bean.ElementBean;
import com.japp.sys.mapper.ElementMapper;
import com.japp.sys.service.ElementService;

/**
 * 
 * @项目名称:中钢期货
 * @类名: ElementAction 
 * @说明: 框架元素管理
 * @作者: Jion
 * @时间: 2013-5-22 下午9:00:07 
 * @版权所属:博思恒信科技有限公司
 * @版本 V1.0
 */
@Scope("prototype")
@Controller
public class ElementAction extends BaseAction<ElementBean> {
	
	private static final long serialVersionUID = -155730993938501274L;
	@Autowired
	private ElementService service;
	@Autowired
	private ElementMapper mapper;
	private ElementBean user;
	private List<ElementBean> list;

	/**
	 * 查询框架页面LEFT
	 * @return
	 */
	public String findFrameLeft(){
		int roleId = Integer.parseInt(session.getAttribute(USERROLE).toString());
		String name = session.getAttribute(USERLOGINID).toString();
		if(roleId==0)return ERROR;
		if(name!=null&&"admin".equals(name))roleId=0;
		json_val = service.queryElementAllToleft(roleId);
		if("];".equals(json_val))json_val="[{id:1,parentId:0,name:'无权限请联系管理员!'}];";
		return "left";
	}
	/**
	 * 查询下拉根据角色ID 返回Json
	 */
	public void findFrameLeftToRoidToJson(){
		json_val = service.queryElementAllJson(Integer.valueOf(key));
		if("];".equals(json_val))json_val="[{id:1,parentId:0,name:'无'}];";
		wPrint(json_val);
	}
	
	/**
	 * 查询框架树形 
	 * @return
	 */
	public void findFrameLeftToJson(){
		list = mapper.queryElementAll(0);
		List<Map<String, Object>> lm = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = null;
		for (ElementBean e:list) {
			map = new HashMap<String, Object>();
			map.put("id", String.valueOf(e.getEm_id()));
			map.put("parentId",  String.valueOf(e.getEm_pid()));
			if(e.getEm_pid()==0){
				map.put("open", "true");
			}
			map.put("name", e.getEm_name());
			lm.add(map);
		}
		JSONArray j = JSONArray.fromObject(lm);
		System.out.println("{\"treeNodes\":"+j.toString()+"}");
		wPrint("{\"treeNodes\":"+j.toString()+"}");
	}
	
	/*=========GET SET 开始==========*/

	public ElementBean getUser() {
		return user;
	}

	public void setUser(ElementBean user) {
		this.user = user;
	}

	public List<ElementBean> getList() {
		return list;
	}

	public void setList(List<ElementBean> list) {
		this.list = list;
	}
	
	/*=========GET SET 结束==========*/
}
